package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.mapper.SpuDetailMapper;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SpuDetailServiceImpl extends ServiceImpl<SpuDetailMapper, SpuDetail>implements SpuDetailService {
   @Autowired
    private SpecParamService specParamService;
    @Autowired
    private SpuService spuService;

    @Override
    public List<SpecParamDTO> querySpecValue(Long id, Boolean searching) {
        //Json转换为map
        Map<Long,Object>specMap= JsonUtils.nativeRead(
                getById(id).getSpecification(), new TypeReference<Map<Long, Object>>() {
                });
        //规格参数的id集合
        Long cid = this.spuService.getById(id).getCid3();
        //根据分类查询对应的规格参数集合
        List<SpecParamDTO>specParamDTOS= this.specParamService.queryParams(null,cid,null);

        specParamDTOS.forEach(specParamDTO -> {
        Long parmaId = specParamDTO.getId();
       //从规格参数map中根据规格参数的id获取对应的值
        specParamDTO.setValue(specMap.get(parmaId));
});

        return specParamDTOS;
    }
}
